package com.factory.test.m3021;

import android.os.Bundle;
import android.widget.CompoundButton;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;

import com.factory.test.R;
import com.factory.test.base.BaseTestActivity;
import com.factory.test.databinding.ActivityM3021RelayBinding;
import com.yecontech.sdk.base.gpio.GpioCustom;
import com.yecontech.sdk.base.gpio.YTGpio;
import com.yecontech.sdk.base.gpio.YTGpioStatusListener;

/**
 * 继电器测试
 */
public class RelayActivity extends BaseTestActivity {

    private ActivityM3021RelayBinding binding;
    private GpioCustom onlineStatus1;
    private GpioCustom lock1Switch;
    private GpioCustom lock1Status;

    private GpioCustom lock2Switch;
    private GpioCustom onlineStatus2;
    private GpioCustom lock2Status;

    private GpioCustom no1Gpio;
    private GpioCustom nc1Gpio;
    private GpioCustom no2Gpio;
    private GpioCustom nc2Gpio;

    private GpioCustom waveStatus;
    private GpioCustom dcStatus;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_m3021_relay);

        lock1Switch = sdkService.getGpioCustomManager();
        lock1Switch.init("103", 0, null);  //1为输入，0为输出
        binding.open1.setChecked(lock1Switch.getAction() == 1);
        binding.open1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                lock1Switch.setAction(isChecked ? 1 : 0);
            }
        });

        lock1Status = sdkService.getGpioCustomManager();
        lock1Status.init("99", 1, new YTGpioStatusListener() {
            @Override
            public void onActive(YTGpio ytGpio, int activeType) {
                handler.post(() -> binding.open1status.setChecked(activeType == 1));
            }
        });
        binding.open1status.setChecked(lock1Status.getAction() == 1);

        //锁在线检测	in	104	todo 低在线，高不在线
        onlineStatus1 = sdkService.getGpioCustomManager();
        onlineStatus1.init("104", 1, new YTGpioStatusListener() {
            @Override
            public void onActive(YTGpio ytGpio, int activeType) {
                handler.post(() -> binding.onlineStatus1.setChecked(activeType == 0));
            }
        });
        binding.onlineStatus1.setChecked(onlineStatus1.getAction() == 0);

        no1Gpio = sdkService.getGpioCustomManager();
        no1Gpio.init("102", 1, new YTGpioStatusListener() {
            @Override
            public void onActive(YTGpio ytGpio, int activeType) {
                handler.post(() -> binding.no1.setChecked(activeType == 1));
            }
        });

        nc1Gpio = sdkService.getGpioCustomManager();
        nc1Gpio.init("101", 1, new YTGpioStatusListener() {
            @Override
            public void onActive(YTGpio ytGpio, int activeType) {
                handler.post(() -> binding.nc1.setChecked(activeType == 1));
            }
        });


        //--------------------------------------
        lock2Switch = sdkService.getGpioCustomManager();
        lock2Switch.init("111", 0, null);  //1为输入，0为输出
        binding.open2.setChecked(lock2Switch.getAction() == 1);
        binding.open2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                lock2Switch.setAction(isChecked ? 1 : 0);
            }
        });

        //锁在线检测	in	112	todo 低在线，高不在线
        onlineStatus2 = sdkService.getGpioCustomManager();
        onlineStatus2.init("112", 1, new YTGpioStatusListener() {
            @Override
            public void onActive(YTGpio ytGpio, int activeType) {
                handler.post(() -> binding.onlineStatus2.setChecked(activeType == 0));
            }
        });
        binding.onlineStatus2.setChecked(onlineStatus2.getAction() == 0);


        lock2Status = sdkService.getGpioCustomManager();
        lock2Status.init("116", 1, new YTGpioStatusListener() {
            @Override
            public void onActive(YTGpio ytGpio, int activeType) {
                handler.post(() -> binding.open2status.setChecked(activeType == 1));
            }
        });
        binding.open2status.setChecked(lock2Status.getAction() == 1);

        no2Gpio = sdkService.getGpioCustomManager();
        no2Gpio.init("110", 1, new YTGpioStatusListener() {
            @Override
            public void onActive(YTGpio ytGpio, int activeType) {
                handler.post(() -> binding.no2.setChecked(activeType == 1));
            }
        });

        nc2Gpio = sdkService.getGpioCustomManager();
        nc2Gpio.init("109", 1, new YTGpioStatusListener() {
            @Override
            public void onActive(YTGpio ytGpio, int activeType) {
                handler.post(() -> binding.nc2.setChecked(activeType == 1));
            }
        });




        waveStatus = sdkService.getGpioCustomManager();
        waveStatus.init("96", 1, new YTGpioStatusListener() {
            @Override
            public void onActive(YTGpio ytGpio, int activeType) {
                handler.post(() -> binding.waveStatus.setChecked(activeType == 1));
            }
        });
        binding.waveStatus.setChecked(waveStatus.getAction() == 1);


        //DC检测
        dcStatus = sdkService.getGpioCustomManager();
        dcStatus.init("3", 1, new YTGpioStatusListener() {
            @Override
            public void onActive(YTGpio ytGpio, int activeType) {
                handler.post(() -> binding.dcStatus.setChecked(activeType == 1));
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeCallbacksAndMessages(null);

        onlineStatus1.release();
        lock1Switch.release();
        lock2Switch.release();

        lock1Status.release();
        lock2Status.release();
        onlineStatus2.release();

        waveStatus.release();
        dcStatus.release();

        no1Gpio.release();
        nc1Gpio.release();

        no2Gpio.release();
        nc2Gpio.release();
    }
}
